package 线程的状态;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2022-11-21
 * Time: 14:23
 */
public class interrupt2 {

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
           while(true){
               //死循环....
               //获取当前线程的打断标记
               boolean interrupted = Thread.currentThread().isInterrupted();
               if(interrupted){
                   //主线程只是给当前线程设置了一个打断标记,至于后续是打断还是继续运行,由当前线程自己决定
                   System.out.println("我被打断了,我退出循环了...");
                   break;
               }
           }
        });
        t.start();//启动t线程
        Thread.sleep(1000);//先让其睡眠1s钟,让其t线程进入睡眠状态
        t.interrupt();//打断正在运行的t线程
        System.out.println("打断完毕");
    }

}
